import java.io.*;
import java.net.Socket;

public class ClientTrans extends Socket{

    private Socket Socket_Client;
    private final String Str_IP="huawei.chisw.net";
    private final int Int_Port = 9999;
    final String Str_FileName ="D:\\passwd";

    public static void main(String[] args) throws Exception
    {

        //创建Socket
        ClientTrans Obj_ClientTrans = new ClientTrans();

        //发送文件信息
        Obj_ClientTrans.Fn_SendMsg(Obj_ClientTrans.Socket_Client);

        //发送文件内容
        Obj_ClientTrans.Fn_SendFile(Obj_ClientTrans.Socket_Client);

        //接收成功消息
        Obj_ClientTrans.Fn_RecvMsg(Obj_ClientTrans.Socket_Client);

        //关闭Socket
        Obj_ClientTrans.Socket_Client.close();
    }

    //构造函数
    public ClientTrans() throws Exception
    {
        this.Socket_Client = new Socket(Str_IP,Int_Port);
        System.out.println("成功连接服务端："+Str_IP);
    }

    //发送文件信息
    public void Fn_SendMsg(Socket Soc) throws Exception
    {
        File File_New = new File(Str_FileName);
        if (!File_New.exists()) {
            return;
        }
        else {
            String Str_Msg = "File@" + Str_FileName + "\r\n";
            BufferedWriter BuffSend = new BufferedWriter(
                    new OutputStreamWriter(Soc.getOutputStream()));

            BuffSend.write(Str_Msg);
            BuffSend.flush();
        }
    }

    //发送文件
    public void Fn_SendFile(Socket Soc) throws Exception
    {
        String Str_Line;

        BufferedReader Buff_File = new BufferedReader(
                new FileReader(Str_FileName));

        BufferedWriter BuffSend = new BufferedWriter(
                new OutputStreamWriter(Soc.getOutputStream()));

        while ((Str_Line=Buff_File.readLine())!=null)
        {
            Str_Line = Str_Line + "\r\n";
            BuffSend.write(Str_Line);
            BuffSend.flush();
            //System.out.print(Str_Line);
        }

        Soc.shutdownOutput();
    }

    //接收成功信息
    public void Fn_RecvMsg(Socket Soc) throws Exception
    {
        BufferedReader BuffRecv = new BufferedReader(
                new InputStreamReader(Soc.getInputStream()));

        System.out.println(BuffRecv.readLine());
        Soc.shutdownInput();
    }


}
